import Vue from 'vue';
import Vuex from 'vuex';
import {
	queryConfigList
} from '@/api/system/tbSysConfig';
import {
	queryPermissionMenu
} from '@/api/system/tbSysMenu';
import {
	getDictData,
	siteBaseInfo,
	getAttachUrl
} from '@/api/openApi';
Vue.use(Vuex)
const state = {
	token: '',
	menuData: [],
	siteInfo: {},
	userInfo: {},
	dictEnums: {},
	dictConst: {},
	authButton: [],
	siteConfig: []
}

const actions = {
	init: ({
		state,
		dispatch
	}) => {
		return new Promise(async (resolve, reject) => {
			await dispatch('getSiteBaseInfo');
			await dispatch('getOpenDictData');
			resolve();
		}).catch(() => {
			reject();
		})
	},

	//获取站点基本信息
	async getSiteBaseInfo({
		commit,
		dispatch
	}) {
		let result = await siteBaseInfo().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? {} : result.data[0];
			if (!!datas.userPic) {
				datas['avatarPath'] = await dispatch('getFileUrl', datas.userPic)
			}
			document.title = datas['name'];
			commit('SET_SITE_INFO', datas);
		}
	},

	//获取权限菜单
	async getPermissionMenu({
		commit
	}) {
		let result = await queryPermissionMenu().then(res => res.data);
		if (result.code === '0') {
			let {
				menuToRouter
			} = require('@/utils/getRouter');
			let datas = !result.data ? {} : result.data[0];
			commit('SET_MENU_DATA', new menuToRouter(datas.menu));
			commit('SET_AUTH_BUTTON', datas.button);
			return datas;
		}
	},

	//获取数据字典数据
	async getOpenDictData({
		commit
	}) {
		let result = await getDictData().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? {} : result.data[0];
			commit('SET_DICT_DATA', datas);
		}
	},

	//获取附件地址
	async getFileUrl({
		commit
	}, id) {
		let result = await getAttachUrl(id).then(res => res.data);
		if (result.code === '0') {
			return !result.data ? null : result.data[0];
		} else {
			Promise.reject(new Error('faile'));
		}
	},

	//获取站点配置
	async getSiteConfig({
		commit
	}) {
		let result = await queryConfigList().then(res => res.data);
		if (result.code === '0') {
			let datas = !result.data ? [] : result.data;
			commit('SET_SITE_CONFIG', datas);
		} else {
			Promise.reject(new Error('faile'));
		}
	}

	/* //获取用户信息
	async getUserInfo({ commit }, data) {
		commit('SET_USER_INFO', data);
	} */
}

const mutations = {
	SET_TOKEN: (state, d) => {
		state.token = d;
		Vue.prototype.$session.set('token', d);
	},
	SET_SITE_INFO: (state, d) => {
		state.siteInfo = d;
		Vue.prototype.$local.set('siteInfo', d);
		// Vue.prototype.$local.set('siteInfo', Vue.prototype.$cryptoEncrypt(d));
	},
	SET_USER_INFO: (state, d) => {
		state.userInfo = d;
		Vue.prototype.$local.set('userInfo', d);
		// Vue.prototype.$local.set('userInfo', Vue.prototype.$cryptoEncrypt(d));
	},
	SET_MENU_DATA: (state, d) => {
		state.menuData = d;
		Vue.prototype.$local.set('menuData', d);
		// Vue.prototype.$local.set('menuData', Vue.prototype.$cryptoEncrypt(d));
	},
	SET_AUTH_BUTTON: (state, d) => {
		state.authButton = d;
		Vue.prototype.$local.set('authButton', d);
		// Vue.prototype.$local.set('authButton', Vue.prototype.$cryptoEncrypt(d));
	},
	SET_DICT_DATA: (state, d) => {
		state.dictEnums = d.enumKey;
		state.dictConst = d.constantKey;
		Vue.prototype.$local.set('dictEnums', d.enumKey);
		Vue.prototype.$local.set('dictConst', d.constantKey);
		// Vue.prototype.$local.set('dictEnums', Vue.prototype.$cryptoEncrypt(d.enumKey));
		// Vue.prototype.$local.set('dictConst', Vue.prototype.$cryptoEncrypt(d.constantKey));
	},
	SET_SITE_CONFIG: (state, d) => {
		state.siteConfig = d;
		Vue.prototype.$local.set('siteConfig', d);
		// Vue.prototype.$local.set('siteConfig', Vue.prototype.$cryptoEncrypt(d));
	}
}

export default {
	state,
	actions,
	mutations
}